package com.apexwear.apexutils.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.PersonHand;
import com.apex.bluetooth.model.EABlePersonInfo;
import com.apexwear.apexutils.R;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.apexwear.apexutils.data.PersonData;
import com.apexwear.apexutils.databinding.ActivityPersonInfoBinding;
import com.apexwear.apexutils.dialog.SelectDialog;
import com.apexwear.apexutils.utils.ShowToast;

public class PersonActivity extends AppCompatActivity {
    final String TAG = this.getClass().getSimpleName();
    ActivityPersonInfoBinding dataBinding;
    PersonData personData;
    PersonObserve personObserve;
    SelectDialog sexDialog, ageDialog, highDialog, weightDialog, wearDialog, skinDialog;
    String[] sexArray, ageArray, highArray, weightArray, wearArray, skinArray;
    int high, weight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_person_info);
        dataBinding.tooBar.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(dataBinding.tooBar);
        getSupportActionBar().setTitle(null);
        dataBinding.tooBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        dataBinding.rowAge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ageArray == null) {
                    ageArray = getResources().getStringArray(R.array.age_array);
                }
                if (ageDialog == null) {
                    ageDialog = new SelectDialog(PersonActivity.this, ageArray, getString(R.string.age));
                    ageDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.age.setText(mac);
                        }
                    });
                }
                if (!ageDialog.isShowing()) {
                    ageDialog.show();
                }

            }
        });
        dataBinding.rowSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sexArray == null) {
                    sexArray = getResources().getStringArray(R.array.sex_array);
                }
                if (sexDialog == null) {
                    sexDialog = new SelectDialog(PersonActivity.this, sexArray, getString(R.string.sex));
                    sexDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.sex.setText(mac);

                        }
                    });
                }
                if (!sexDialog.isShowing()) {
                    sexDialog.show();
                }
            }
        });
        dataBinding.rowHigh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (highArray == null) {
                    highArray = getResources().getStringArray(R.array.high_array);
                }
                if (highDialog == null) {
                    highDialog = new SelectDialog(PersonActivity.this, highArray, getString(R.string.high) + "(" + getString(R.string.unit) + getString(R.string.cm) + ")");
                    highDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            high = Integer.valueOf(mac);
                            dataBinding.high.setText(mac + getString(R.string.cm));
                        }
                    });
                }
                if (!highDialog.isShowing()) {
                    highDialog.show();
                }
            }
        });
        dataBinding.rowWeight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weightArray == null) {
                    weightArray = getResources().getStringArray(R.array.weight_array);
                }
                if (weightDialog == null) {
                    weightDialog = new SelectDialog(PersonActivity.this, weightArray, getString(R.string.weight) + "(" + getString(R.string.unit) + getString(R.string.kg) + ")");
                    weightDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            weight = Integer.valueOf(mac) * 1000;
                            dataBinding.weight.setText(mac + getString(R.string.kg));
                        }
                    });
                }
                if (!weightDialog.isShowing()) {
                    weightDialog.show();
                }
            }
        });
        dataBinding.rowWear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wearArray == null) {
                    wearArray = getResources().getStringArray(R.array.wear_array);
                }
                if (wearDialog == null) {
                    wearDialog = new SelectDialog(PersonActivity.this, wearArray, getString(R.string.wear));
                    wearDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.wear.setText(mac);
                        }
                    });
                }
                if (!wearDialog.isShowing()) {
                    wearDialog.show();
                }
            }
        });
        dataBinding.rowSkin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (skinArray == null) {
                    skinArray = getResources().getStringArray(R.array.skin_array);
                }
                if (skinDialog == null) {
                    skinDialog = new SelectDialog(PersonActivity.this, skinArray, getString(R.string.skin));
                    skinDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.skin.setText(mac);
                        }
                    });
                }
                if (!skinDialog.isShowing()) {
                    skinDialog.show();
                }
            }
        });
        personData = new ViewModelProvider(PersonActivity.this).get(PersonData.class);
        dataBinding.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sex = dataBinding.sex.getText().toString();
                String age = dataBinding.age.getText().toString();
                String hight = dataBinding.high.getText().toString();
                String weigh = dataBinding.weight.getText().toString();
                String wear = dataBinding.wear.getText().toString();
                String skin = dataBinding.skin.getText().toString();
                if (TextUtils.isEmpty(sex) || TextUtils.isEmpty(age) || TextUtils.isEmpty(hight) || TextUtils.isEmpty(weigh)) {
                    ShowToast.show(PersonActivity.this, getString(R.string.person_info_hint));
                    return;
                }
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABlePersonInfo eaBlePersonInfo = new EABlePersonInfo();
                    eaBlePersonInfo.setAge(Integer.valueOf(age));
                    eaBlePersonInfo.setHeight(high);
                    eaBlePersonInfo.setWeight(weight);
                    if (sex.equalsIgnoreCase(getString(R.string.female))) {
                        eaBlePersonInfo.setE_sex_info(EABlePersonInfo.PersonSex.female);
                    } else {
                        eaBlePersonInfo.setE_sex_info(EABlePersonInfo.PersonSex.male);
                    }
                    if (!TextUtils.isEmpty(wear) && wear.equalsIgnoreCase(getString(R.string.right_hand))) {
                        eaBlePersonInfo.setE_hand_info(PersonHand.right);
                    } else {
                        eaBlePersonInfo.setE_hand_info(PersonHand.left);
                    }
                    if (TextUtils.isEmpty(skin)) {
                        eaBlePersonInfo.setE_skin_color(EABlePersonInfo.SkinColor.skin_yellow);
                    } else if (skin.equalsIgnoreCase(getString(R.string.skin_yellow_black))) {
                        eaBlePersonInfo.setE_skin_color(EABlePersonInfo.SkinColor.skin_yellow_black);
                    } else if (skin.equalsIgnoreCase(getString(R.string.skin_yellow))) {
                        eaBlePersonInfo.setE_skin_color(EABlePersonInfo.SkinColor.skin_yellow);
                    } else if (skin.equalsIgnoreCase(getString(R.string.skin_black))) {
                        eaBlePersonInfo.setE_skin_color(EABlePersonInfo.SkinColor.skin_balck);
                    } else if (skin.equalsIgnoreCase(getString(R.string.skin_white))) {
                        eaBlePersonInfo.setE_skin_color(EABlePersonInfo.SkinColor.skin_white);
                    } else if (skin.equalsIgnoreCase(getString(R.string.skin_white_yellow))) {
                        eaBlePersonInfo.setE_skin_color(EABlePersonInfo.SkinColor.skin_white_yellow);
                    }
                    personData.setPersonInfo(eaBlePersonInfo).observe(PersonActivity.this, new Observer<Boolean>() {
                        @Override
                        public void onChanged(Boolean aBoolean) {
                            if (aBoolean) {
                                ShowToast.show(PersonActivity.this, getString(R.string.add_success));
                            } else {
                                ShowToast.show(PersonActivity.this, getString(R.string.add_fail));
                            }
                        }
                    });
                }

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (personObserve == null) {
            personObserve = new PersonObserve();
        }
        personData.getPersonInfo().observe(PersonActivity.this, personObserve);
    }

    class PersonObserve implements Observer<EABlePersonInfo> {

        @Override
        public void onChanged(EABlePersonInfo eaBlePersonInfo) {
            if (eaBlePersonInfo != null) {
                high = eaBlePersonInfo.getHeight();
                weight = eaBlePersonInfo.getWeight();
                dataBinding.age.setText(eaBlePersonInfo.getAge() + "");
                dataBinding.sex.setText(eaBlePersonInfo.getE_sex_info() == null ? "" : (eaBlePersonInfo.getE_sex_info() == EABlePersonInfo.PersonSex.female ? getString(R.string.female) : getString(R.string.male)));
                dataBinding.high.setText(eaBlePersonInfo.getHeight() == 0 ? "" : (eaBlePersonInfo.getHeight() + getString(R.string.cm)));
                dataBinding.weight.setText(eaBlePersonInfo.getWeight() == 0 ? "" : (eaBlePersonInfo.getWeight() / 1000 + getString(R.string.kg)));
                dataBinding.wear.setText(eaBlePersonInfo.getE_hand_info() == null ? "" : (eaBlePersonInfo.getE_hand_info() == PersonHand.left ? getString(R.string.left_hand) : getString(R.string.right_hand)));
                EABlePersonInfo.SkinColor skinColor = eaBlePersonInfo.getE_skin_color();
                if (skinColor == null) {
                    dataBinding.skin.setText("");
                } else if (skinColor == EABlePersonInfo.SkinColor.skin_balck) {
                    dataBinding.skin.setText(getString(R.string.skin_black));
                } else if (skinColor == EABlePersonInfo.SkinColor.skin_white) {
                    dataBinding.skin.setText(getString(R.string.skin_white));
                } else if (skinColor == EABlePersonInfo.SkinColor.skin_yellow) {
                    dataBinding.skin.setText(getString(R.string.skin_yellow));
                } else if (skinColor == EABlePersonInfo.SkinColor.skin_white_yellow) {
                    dataBinding.skin.setText(getString(R.string.skin_white_yellow));
                } else if (skinColor == EABlePersonInfo.SkinColor.skin_yellow_black) {
                    dataBinding.skin.setText(getString(R.string.skin_yellow_black));
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (sexDialog != null) {
            sexDialog.destroyDialog();
            sexDialog = null;
        }
        if (ageDialog != null) {
            ageDialog.destroyDialog();
            ageDialog = null;
        }
        if (highDialog != null) {
            highDialog.destroyDialog();
            highDialog = null;
        }
        if (weightDialog != null) {
            weightDialog.destroyDialog();
            weightDialog = null;
        }
        if (wearDialog != null) {
            wearDialog.destroyDialog();
            wearDialog = null;
        }
        if (skinDialog != null) {
            skinDialog.destroyDialog();
            skinDialog = null;
        }
        sexArray = null;
        ageArray = null;
        highArray = null;
        weightArray = null;
        wearArray = null;
        skinArray = null;
        personObserve = null;
        personData = null;
        if (dataBinding != null) {
            dataBinding.unbind();
            dataBinding = null;
        }
        super.onDestroy();
    }
}
